/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Controller.ReservationController;
import agh.rozproszony.model.Invitation;
import agh.rozproszony.model.Reservation;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.AbstractTableModel;
import View.InvitationRow;

/**
 *
 * @author Marcin_PC
 */
public class InvitationsTable extends AbstractTableModel {

    private final static Object[] columnNames = {"Zapraszający", "Dzien", "Godzina", "Temat", "Zaakceptuj"};
    private List<InvitationRow> rows = new ArrayList<InvitationRow>();
    private final static int INVITER_IDX = 0;
    private final static int DATE_IDX = 1;
    private final static int HOUR_IDX = 2;
    private final static int TITLE_IDX = 3;
    private final static int ACCEPT_IDX = 4;

    @Override
    public int getRowCount() {
        if (rows == null) {
            return 0;
        }
        return rows.size();
    }

    @Override
    public int getColumnCount() {
        return columnNames.length;
    }

    @Override
    public String getColumnName(int column) {
        return columnNames[column].toString();
    }

    @Override
    public Object getValueAt(int rowIndex, int columnIndex) {
        if (rows == null) {
            return null;
        }
        InvitationRow row = (InvitationRow) rows.get(rowIndex);
        switch (columnIndex) {
            case INVITER_IDX:
                return row.getInviter();
            case DATE_IDX:
                return row.getDate();
            case HOUR_IDX:
                return row.getHour();
            case TITLE_IDX:
                return row.getTitle();
            case ACCEPT_IDX:
                return row.getAccepted();
            default:
                return row;
        }
    }

    @Override
    public Class<?> getColumnClass(int columnIndex) {

        switch (columnIndex) {
            case INVITER_IDX:
                return String.class;
            case DATE_IDX:
                return String.class;
            case HOUR_IDX:
                return String.class;
            case TITLE_IDX:
                return String.class;
            case ACCEPT_IDX:
                return Boolean.class;
            default:
                return Boolean.class;
        }
    }

    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex) {
        switch (columnIndex) {
            case ACCEPT_IDX: {
                rows.get(rowIndex).setAccepted(!rows.get(rowIndex).getAccepted());
                return true;
            }
            default:
                return false;
        }
    }

    public void addRow(List<Invitation> invitations, ReservationController controller) throws RemoteException {
        for (int i = 0; i < invitations.size(); i++) {
            Reservation rsv = controller.getReservation(invitations.get(i).getReservationId());

            InvitationRow invitationRow = new InvitationRow(invitations.get(i), 
                    controller.getReservation(invitations.get(i).getReservationId()),
                    controller.getUserNameById(rsv.getOwnerId()).getLogin());
            rows.add(invitationRow);
        }

    }

    public void clear() {
        rows.clear();
    }
    /*
     * Return selected invitations to remove InvitationRow
     */
    public void removeUnselectedRows()
    {
        ArrayList<InvitationRow> toRemove = new ArrayList<InvitationRow>();
        
        for( InvitationRow row : rows )
        {
            if( row.getAccepted() == false )
            {
               toRemove.add(row);
            }
        }
        for( InvitationRow remove : toRemove )
        {
            rows.remove(remove);
        }
    }
    
     public void removeSelectedRows()
    {
        ArrayList<InvitationRow> toRemove = new ArrayList<InvitationRow>();
        
        for( InvitationRow row : rows )
        {
            if( row.getAccepted() == true )
            {
               toRemove.add(row);
            }
        }
        for( InvitationRow remove : toRemove )
        {
            rows.remove(remove);
        }
    }
    
    /**
     *
     * @return
     */
    public ArrayList<InvitationRow> getAllRows()
    {
        ArrayList<InvitationRow> result = new ArrayList<InvitationRow>();
               
        result= deepCopyList(rows);
        
        return result;
    }
    
    public ArrayList<InvitationRow> deepCopyList(List<InvitationRow> toCopy)
    {
        ArrayList<InvitationRow> copied = new ArrayList<InvitationRow>();
        
        for(int i=0;i<toCopy.size();i++)
        {
        
        copied.add(new InvitationRow(toCopy.get(i).getInviter(),toCopy.get(i).getHour(),toCopy.get(i).getTitle(),toCopy.get(i).getDate(),toCopy.get(i).getAccepted(),toCopy.get(i).getReservationId()));
        
        }
        
        return copied;
    }
}
